package com.slz3.business.module.orderManagement.ordOrderLogistics.domain;

import com.slz3.business.frame.enums.AuditFlag;
import com.slz3.business.frame.enums.UsingFlag;
import com.slz3.business.frame.redis.Jedis;
import com.slz3.business.frame.redis.RK;
import com.slz3.business.frame.steward.Steward;
import com.slz3.business.frame.util.LocalDT;
import com.slz3.business.frame.util.UUIDUtils;
import com.slz3.business.frame.util.VE;
import com.slz3.business.module.orderManagement.ordOrder.domain.OrdOrder;
import com.slz3.business.module.orderManagement.ordOrder.domain.Order;
import com.slz3.business.module.orderManagement.ordOrder.domain.OrderDetailGroup;
import org.springframework.util.MultiValueMap;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

public class OrdOrderLogisticsFactory {

    /**
     * 初始化基础属性值
     */
    public static OrdOrderLogistics initBaseFields(OrdOrderLogistics ordOrderLogistics) {
        if (ordOrderLogistics.getUuid() == null) {
            ordOrderLogistics.setUuid(UUIDUtils.generateUUID());
            ordOrderLogistics.setCreateTime(LocalDateTime.now());
            if (VE.isEmpty(ordOrderLogistics.getSerAudit())) {
                ordOrderLogistics.setSerAudit(AuditFlag.WAIT_SUBMIT);
            }
            if (VE.isEmpty(ordOrderLogistics.getSerUsing())) {
                ordOrderLogistics.setSerUsing(UsingFlag.ENABLE);
            }
        } else {
            ordOrderLogistics.setLastModifyTime(LocalDateTime.now());
        }
        return ordOrderLogistics;
    }

    /**
     * 初始化基础属性值
     */
    public static OrdOrderLogistics initBaseFields() {
        return initBaseFields(newTemStyle());
    }

    public static OrdOrderLogistics newTemStyle() {
        return new OrdOrderLogistics();
    }

    public static OrdOrderLogisticsCondition newOrdOrderLogisticsCondition(MultiValueMap<String, String> params) {

        OrdOrderLogisticsCondition condition = new OrdOrderLogisticsCondition();

        String createTimeOUTSET = params.getFirst("createTimeOUTSET");
        String createTimeFINISH = params.getFirst("createTimeFINISH");
        String lastModifyTimeOUTSET = params.getFirst("lastModifyTimeOUTSET");
        String lastModifyTimeFINISH = params.getFirst("lastModifyTimeFINISH");
        String serAuditList = params.getFirst("serAuditList");
        String orderUuid = params.getFirst("orderUuid");
        condition.setCreateTimeOUTSET(VE.isEmpty(createTimeOUTSET) ? null : LocalDT.parseStringToDateTime(createTimeOUTSET, OrdOrderLogisticsCondition.CREATE_TIME_OF_PATTERN));
        condition.setCreateTimeFINISH(VE.isEmpty(createTimeFINISH) ? null : LocalDT.parseStringToDateTime(createTimeFINISH, OrdOrderLogisticsCondition.CREATE_TIME_OF_PATTERN));
        condition.setLastModifyTimeOUTSET(VE.isEmpty(lastModifyTimeOUTSET) ? null : LocalDT.parseStringToDateTime(lastModifyTimeOUTSET, OrdOrderLogisticsCondition.CREATE_TIME_OF_PATTERN));
        condition.setLastModifyTimeFINISH(VE.isEmpty(lastModifyTimeFINISH) ? null : LocalDT.parseStringToDateTime(lastModifyTimeFINISH, OrdOrderLogisticsCondition.CREATE_TIME_OF_PATTERN));
        condition.setSerAuditList(VE.isEmpty(serAuditList) ? null : Arrays.stream(serAuditList.split(",")).map(item -> AuditFlag.valueOf(item)).collect(Collectors.toList()));
        if(!VE.isEmpty(orderUuid)) {
            condition.setOrderUuid(UUID.fromString(orderUuid));
        }
        return condition;

    }

    public static void syrcRedisOrder(OrdOrder ordOrder){

        String orderInfoKey = RK.SERVICE__ORDER__INFO__ORDER_UUID__$UUID$.replace("${uuid}",ordOrder.getUuid().toString());

        //先获取redis订单
        Order order = Order.builder().build();
        Map<String,Object> orderInfoMap = Jedis.db0.getMap(orderInfoKey);
        if(VE.isNotEmpty(orderInfoMap)){
            Steward.map2Obj(orderInfoMap,order);

            order.setSerOrderStatus(ordOrder.getSerOrderStatus().toString());
            order.setSerDeliveryTime(LocalDT.getDateTimeAsString(ordOrder.getSerDeliveryTime(),"yyyy-MM-dd HH:mm:ss"));

            //保存order
            Jedis.db0.set(orderInfoKey,Steward.obj2Map(order));

            String orderJoinOrderDetailGroupKey = RK.SERVICE__ORDER__JOIN__ORDER_DETAIL_GROUP__ORDER_UUID__$UUID$.replace("${uuid}",order.getSerOrderUuid());
            String orderGroupSortWaitDeliverPageKey = RK.SERVICE__ORDER_DETAIL_GROUP__SORT__WAIT_DELIVER__MEMBER_UUID__$UUID$.replace("${uuid}",order.getSerMemberUuid());
            String orderGroupSortWaitReceiverPageKey = RK.SERVICE__ORDER_DETAIL_GROUP__SORT__WAIT_RECEIVER__MEMBER_UUID__$UUID$.replace("${uuid}",order.getSerMemberUuid());


            //修改订单详情分组 分页
            List<String> orderDetailGroupInfoKeyList = Jedis.db0.getSet(orderJoinOrderDetailGroupKey)
                    .stream().filter(item->item!=null)
                    .map(item->RK.SERVICE__ORDER_DETAIL_GROUP__INFO__ORDER_DETAIL_GROUP_UUID__$UUID$.replace("${uuid}",item.toString())).collect(Collectors.toList());

            List<OrderDetailGroup> orderDetailGroupList = Jedis.db0.getListByKey(orderDetailGroupInfoKeyList)
                    .stream().filter(item->item!=null).map(item->{
                        OrderDetailGroup orderDetailGroup = OrderDetailGroup.builder().build();
                        Map<String,Object> orderDetailGroupMap = (Map)item;
                        Steward.map2Obj(orderDetailGroupMap,orderDetailGroup);
                        return orderDetailGroup;
                    }).collect(Collectors.toList());

            orderDetailGroupList.forEach(orderDetailGroup -> {
                orderDetailGroup.setSerOrderStatus(order.getSerOrderStatus());
                orderDetailGroup.setSerDeliveryTime(order.getSerDeliveryTime());
                Jedis.db0.removeFromList(orderGroupSortWaitDeliverPageKey,0,orderDetailGroup.getUuid());
                Jedis.db0.lpush(orderGroupSortWaitReceiverPageKey,orderDetailGroup.getUuid());
                Jedis.db0.set(RK.SERVICE__ORDER_DETAIL_GROUP__INFO__ORDER_DETAIL_GROUP_UUID__$UUID$.replace("${uuid}",orderDetailGroup.getUuid()),Steward.obj2Map(orderDetailGroup));
            });
        }

    }

}
